package C036;

/**
 * ClassName: C09
 * Package: C036
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/21 21:40
 * @Version 1.0
 * @Since 1.0
 */
public class C09 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode2.left = treeNode4;
        treeNode2.right = treeNode5;
        treeNode3.left = treeNode6;
        System.out.println(isCompleteTree(treeNode1));
    }
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    static int MAXLEN = 100;
    static TreeNode[] queue = new TreeNode[MAXLEN];
    static int r, l ;
    // 是否是完全二叉树
    public static boolean isCompleteTree(TreeNode root) {
        if(root == null) {
            return true;
        }
        r = l = 0;
        queue[r++] = root;
        TreeNode cur;
        boolean flag = true;
        while(l < r) {
            cur = queue[l++];
            if(cur.right != null && cur.left == null) {
                return false;
            }
            if(!flag && (cur.left != null || cur.right != null)) {
                return false;
            }
            if(cur.left == null || cur.right == null) {
                flag = false;
            }
            if(cur.left != null) {
                queue[r++] = cur.left;
            }
            if(cur.right != null) {
                queue[r++] = cur.right;
            }
        }
        return true;
    }
}
